<?php
/**
 * Uni-Form widget to add needed css and javascript files on page
 * 
 * @author Alexander Hramov
 * @link http://www.hramov.info
 * @version 0.2
 */
class UniForm extends CWidget
{
  protected $dirSeparator = DIRECTORY_SEPARATOR;
  protected $cssDir = 'css';
  protected $jsDir = 'js';
  protected $imageDir = 'images';
  protected $widgetCssFiles = array('uni-form.css'=>true, 'uni-form-generic.css'=>false);
  protected $widgetJsFiles = array('uni-form.jquery.js');
  protected $widgetImages = array();

  public function init()
  {
    $cs = Yii::app()->getClientScript();
    $cs->registerCoreScript('jquery');

    $am = Yii::app()->getAssetManager();

    // Publish css
    foreach($this->widgetCssFiles as $cssFile=>$isReg)
    {
      $tempCss = $am->publish(dirname(__FILE__).$this->dirSeparator.$this->cssDir.$this->dirSeparator.$cssFile);
      if ($isReg)
        $cs->registerCssFile($tempCss);
    }

    // Publish images
    foreach($this->widgetImages as $key=>$image)
      $this->widgetImages[$key] = $am->publish(dirname(__FILE__).$this->dirSeparator.$this->imagesDir.$this->dirSeparator.$image);

    // Publish scripts
    foreach($this->widgetJsFiles as $jsFile)
      $cs->registerScriptFile($am->publish(dirname(__FILE__).$this->dirSeparator.$this->jsDir.$this->dirSeparator.$jsFile));
      
  }

  public function run()
  {
    CHtml::$requiredCss = '';
    CHtml::$afterRequiredLabel='';
    CHtml::$beforeRequiredLabel='<em>*</em> '; 
    CHtml::$errorSummaryCss = 'errorMsg';
    $cs = Yii::app()->getClientScript();
    $cs->registerScript('uniform_function_ready', "jQuery('form.uniForm').uniform();", CClientScript::POS_READY);
  }

}

?>